package pro.cherish.smartfile.component;

import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import pro.cherish.smartfile.config.AppContext;
import pro.cherish.smartfile.module.entity.Attachment;
import pro.cherish.smartfile.service.AttachmentService;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author 创建人：lys
 * @version 版本号：V1.0
 * <p>
 * ***************************修订记录************************************
 * 2020-12-04 lys 创建该类功能。
 * **********************************************************************
 * </p>
 * @ClassName 类名：FastUploadHelper
 * @Description 功能说明：
 * <p>
 *
 * </p>
 * **********************************************************************
 * @date 创建日期： 2020-12-04 lys
 */
@Slf4j
public class FastUploadHelper {

    private static FastUploadHelper fastUploadHelper;

    @Resource
    private AttachmentService attachmentService;

    public FastUploadHelper() {
        AppContext.getApplicationContext().getAutowireCapableBeanFactory().autowireBean(this);
    }

    public synchronized static FastUploadHelper getInstance() {
        if (FastUploadHelper.fastUploadHelper == null) {
            return new FastUploadHelper();
        } else {
            return FastUploadHelper.fastUploadHelper;
        }
    }
    /**
     * 校验文件是否已经上传,校验标准：db和本地
     *
     * @param identify
     */
    public Boolean isExistArchiveWithDbAndLocal(String identify) {
        Attachment archive = getArchiveWithDb(identify);
        if (ObjectUtil.isNotNull(archive)) {
            Date updateTime = archive.getUpdateTime();
            return FileHelper.getInstance().existArchive(updateTime, identify);
        }else {
            return false;
        }
    }
    public Attachment getArchiveWithDb(String identify) {
         return attachmentService.queryFile(identify);
    }
}
